package com.malou.client.ventas.colores;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;

public class ColorDialog extends Dialog {

	protected HiddenField<String> id;
	protected TextField<String> descripcion;
	protected Button guardar;
	protected Button cancelar;
	protected Status status;

	private FormBinding formBindings;
	Color tp;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public ColorDialog(){
		setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		setSize(300, 50);
		setClosable(true);
		
		FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	    
	    formBindings = new FormBinding(panel, false);	    
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(descripcion);
	}
	
	 @Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  ColorDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.guardar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
  		    getButtonBar().disable();
  		    
  			tp = new Color();
  			
  			if(id.getValue() != null)
  				tp.setId(Integer.valueOf(id.getValue()));
  			tp.setDescripcion(descripcion.getValue());
  			
  			Colores.colorDialog = ColorDialog.this;   			
  			Colores.save(tp);	    			
	    	}	    		 
	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	  }
	 
	 protected boolean validate() {
			boolean nom = descripcion.isValid();
		    return nom;
		  }
	 
	 public void reset(){
		  setIconStyle("icon-new");
		  setHeading(constants.coloresNuevoColor());
		  		  
		  id.clear();
		  descripcion.clear();
		  status.hide();
		  getButtonBar().enable();
	  }
	 
	 public void setStore(ListStore<BeanModel> store){		  
		  formBindings.addFieldBinding(new FieldBinding(id, "id"));
		  formBindings.addFieldBinding(new FieldBinding(descripcion, "descripcion"));
		  formBindings.setStore(store);
	  }
	 
	 private void addFields(ContentPanel cp) {

			id = new HiddenField<String>();
			id.setVisible(false);
			cp.add(id);
			
			descripcion = new TextField<String>();
		    descripcion.setMinLength(3);
		    descripcion.setFieldLabel(constants.coloresDescripcion());	    
		    descripcion.setAllowBlank(false);	    
		    cp.add(descripcion, new FormData("-20"));
 
	   }
	 
	 public void beforeEdit(){
		  setIconStyle("icon-update");
		  setHeading(constants.coloresModificarColor());		  
		  
		  status.hide();
		  getButtonBar().enable();
	  }
	  public void bind(BeanModel selectedModel){		  
		  formBindings.bind(selectedModel);
	  }
	  public void unbind(){
		  formBindings.unbind();
	  }
}
